Uec3.PickExpressModule = Ext.extend(Ext.app.Module,{
	
	id:'pickExpress',		
		
	getComponent:function(params){
	
		var module = this;
		
		var source_combo = Uec3.App.dictionary.getCombo("part_source",{hiddenName:'source',fieldLabel:'来源'});
		
		var ds_code_select = new Ext.data.JsonStore({
	    	 	url:Uec3.App.Constants.homeUrl+"/part/queryPart!getPartByCodeImplicitExt.do",
	     		root:'root',
	     		totalProperty:'totalProperty',
        		fields:[{name:'code',type:'string'},{name:'name',type:'string'}]
	 	});
		
	 	var store_ds = new Ext.data.JsonStore({
				url : Uec3.App.Constants.homeUrl+ '/store/base!query.do',
				root:'root',
				totalProperty: "totalProperty",
				fields:[
					{name : 'id',type:'int'},
					{name : 'name',type:'string'},
					{name : 'pyCode',type:'string'},
					{name : 'departmentName',type:'string'}
				]
		});
	 	
		var resultTpl = new Ext.XTemplate(
            '<tpl for="."><div class="search-item">',
               '{name},{departmentName}',             
            '</div></tpl>'
        );
		
	 	source_combo.on("select",autoFillDetailForm);
	 	
	 	var ds_partner_store = new Ext.data.JsonStore({
			url : Uec3.App.Constants.homeUrl+ "/partner/query_partner!queryPartner.do",
			root : 'root',
			totalProperty : 'totalProperty',
			remoteSort : true,
			baseParams : {start : 0,limit : 4,type : 1},
			fields : [
				{name : 'id',type : 'int'},
				{name : 'pyCode',type : 'string'},
				{name : 'name',type : 'string'},
				{name : 'companyId',type : 'int'}]
		});

		// 供货商combox
		var partner_combox = new Ext.form.LookupBox2({
			fieldLabel:'供货商',
			store : ds_partner_store,
			displayField : 'name',
			lookupField : 'pyCode',
			valueField : 'id',	
			name : 'partnerName',
			typeAhead : false,
			mode : 'remote',
			triggerAction : 'all',
			emptyText : '选择供货商..',
			selectOnFocus : true,
			queryParam : 'queryParam',
			lookupCallBackEvents : ['lookupChange'],
			queryDelay : 10,
			typeAheadDelay : 10,
			pageSize : 4,
			allowBlank:false,
			start : 0,
			width : 270,
			anchor : '100%',
			app : this.app,
			listWidth:250,
			triggerTags : [{moduleId : 'partner_query',triggerClass : 'x-form-search-trigger',params : {partnerType : 1}}, {moduleId : 'partner_add'}],
			listeners : {
				'select' : function() {
					search_form.getForm().findField('sellerId').setValue((this.getStore().getById(this.getValue())).data.companyId);
				},
				'lookupChange' : function(record) {
					search_form.getForm().findField('sellerId').setValue(record.data.companyId);
				}
			}
		});
	 	
	 	var store_lookup = new Ext.form.LookupBox2({
		 	fieldLabel : '仓库',
			itemId:'store_lookup',
			store: store_ds,
			displayField:'name',
			lookupField:'pyCode',
			name:'storeName',
			valueField:'id',
			allowBlank:false,
			typeAhead: true,
			tpl: resultTpl,
			itemSelector: 'div.search-item',
			mode: 'remote',
			triggerAction: 'all',
			emptyText:'请选择仓库...',
			selectOnFocus:true, 			
			queryDelay:10,
			typeAheadDelay:10,
			pageSize:5,
			autoDestory:true,
			queryParam:'keyword',
			app:this.app,
			listWidth:250,
			triggerTags:[{moduleId: 'store_list',triggerClass:'x-form-search-trigger'},{moduleId: 'store_add'}]
	 	});
	 	
		var search_form = new Ext.FormPanel({
			frame:true,
			border:false,
			items:[
			  {
			   xtype:'fieldset',
    	       title:'零件属性信息',
    	       items:[
			    {
			     layout:'column',			    
			     items:[
			       {
			         columnWidth:0.33,
			         layout:'form',
			         labelWidth:53,			        
			         defaults:{anchor:'100%',readOnly:true,style:{background:'#dddddd'}},
			         items:[			           
			           {
			           	 xtype:'combo',	
			           	 allowBlank : false,
						 blankText : '零件编码不能为空',
						 name : 'code',					
						 fieldLabel:'零件编码',
					     valueField:'code',
	                     displayField : 'code',
   	  					 mode : 'remote',
   	  					 queryParam :'queryParam',
   	  					 queryDelay:500,
   	  					 minChars :2,
   	  					 store : ds_code_select,  	  								 
   	  					 pageSize:10,
   	  					 readOnly:false,
   	  					 style:{},
	   					 selectOnFocus : true,
	   					 triggerAction : 'all',
	   					 loadingText : '加载中...',	   				
	   				     emptyText : '请输入至少2位零件编码',
	   				     listWidth:270, 
	   				     listeners :{
					         'select':function(){
					   	           var value = this.getValue();
					      		   search_form.getForm().el.dom.reset();
					      		  // inventory_grid_store.removeAll();
					               this.setValue(value);
					          },
					          'change':function(){
					   	           var value = this.getValue();
					               search_form.getForm().el.dom.reset();
					               //inventory_grid_store.removeAll();
					               this.setValue(value);
					          }
					      } 
			           },
			           {xtype:'textfield',fieldLabel:'品牌',name:'brand'},
			           {xtype:'numberfield',fieldLabel:'媒体价',name:'retailPrice'}			          
			         ]
			       },{
			       	 columnWidth:0.33,
			         layout:'form',
			         labelWidth:53,
			         defaults:{anchor:'99%', labelStyle:"padding-left:5px"},
			         items:[
			           source_combo,			           
			           {xtype:'textfield',fieldLabel:'车型',name:'carModel',readOnly:true,style:{textLeft:'3px',background:'#dddddd'}},
			           {xtype:'numberfield',fieldLabel:'保险价',name:'insurancePrice',readOnly:true,style:{background:'#dddddd'}}			           
			         ]
			       },{
			       	columnWidth:0.33,
			       	layout:'form',
			       	labelWidth:53,
			       	defaults:{labelStyle:"padding-left:5px",anchor:'99%',readOnly:true,style:{background:'#dddddd'}},
			       	items:[	
			       	   //{xtype:'button',text:'查找',width:100,style:{marginLeft:'61px',marginBottom:'3px'},handler:searchInventory},
			       	   {xtype:'textfield',fieldLabel:'名称',name:'name'},
			       	   {xtype:'textfield',fieldLabel:'单位',name:'measurementUnit'},
			       	   {xtype:'numberfield',fieldLabel:'最低价',name:'minPrice'},			       	  
			       	   {xtype:'hidden',name:'origin'},
			       	   {xtype:'hidden',name : 'partId'}
			       	]
			       }
			     ]
			   }
			 ]
		    },{
		      xtype:'fieldset',
    	      title:'急件信息',
    	      items:[
    	       {
    	       	layout:'column',
    	       	items:[
    	       	 {
    	       	   columnWidth:0.33,
			       layout:'form',
			       labelWidth:53,			        
			       defaults:{anchor:'100%'},
			       items:[
			       	 {xtype:'numberfield',fieldLabel:'进价',name:'cost',allowBlank: false,allowNegative: false},
			         {xtype:'numberfield',fieldLabel:'订购数量',name:'quantity',readOnly:true,style:{background:'#dddddd'}}
			       ]
    	       	 },{
    	       	 	columnWidth:0.33,
			        layout:'form',
			        labelWidth:53,
			        defaults:{anchor:'99%', labelStyle:"padding-left:5px"},
			        items:[
			          store_lookup,
			          {xtype:'numberfield',fieldLabel:'急件数量',name:'buyQuantity',
			           listeners:{
			           	  'blur':function(){
			           	  	 search_form.getForm().findField('saleQuantity').setValue(this.getValue());
			           	  }
			           }
			          }
			        ]
    	       	 },{
    	       	 	columnWidth:0.33,
			       	layout:'form',
			       	labelWidth:53,
			       	defaults:{labelStyle:"padding-left:5px",anchor:'99%'},
			       	items:[
			       	  {xtype:'textfield',name:'shelf',fieldLabel:'仓位'},
			       	  {xtype:'numberfield',fieldLabel:'出库数量',name:'saleQuantity',readOnly:true,style:{background:'#dddddd'}}
			       	]
    	       	 }
    	       	]
    	       }
    	      ]
		    },{
		      xtype:'fieldset',
    	      title:'供应商信息',
    	      items:[
    	        {
    	         layout:'column',			    
			     items:[
			      {
			      	columnWidth:0.33,
			        layout:'form',
			        labelWidth:53,			        
			        defaults:{anchor:'100%'},
					items:[partner_combox]
			      },{
			      	columnWidth:0.33,
			        layout:'form',
			        labelWidth:53,
			        defaults:{anchor:'99%', labelStyle:"padding-left:5px"},
					items:[{xtype:'numberfield',name:'codeOfSeller',fieldLabel:'卖方单据'}]
			      },{
			      	columnWidth:0.33,
			       	layout:'form',
			       	labelWidth:53,
			       	defaults:{anchor:'99%',labelStyle:"padding-left:5px"},
			       	items:[{xtype:'textfield',name:'memo',fieldLabel:'备注'},
			       	       {xtype:'hidden',name : 'sellerId'}]
			      }
			     ]
    	        }
    	      ]
		    }
	      ],
	      buttons:[
			     {			     
			        handler:submitForm,
			        text:'确定',
			        iconCls:'icon-save'
			     },{
			     	text:'取消',
			     	handler:function(){				     		
			     	   search_form.ownerCt.close();
			     	}
			     }
			 ],
			 buttonAlign:'center'
			
		});
		
		function submitForm(){
			search_form.enable();
			var record = search_form.getForm().getValues();		
			var sourceName = search_form.getForm().findField("source").getRawValue();	      
	        var storeId = store_lookup.value;
	        var storePyCode = store_lookup.lookupValue;		       
	        var partnerId = partner_combox.value;	       
	        var partnerPyCode = partner_combox.lookupValue;
	        record.sourceName = sourceName;
	        record.storeId = storeId;
			record.storePyCode = storePyCode;
			record.partnerId = partnerId;
			record.partnerPyCode = partnerPyCode;
			if(params.callback && Ext.isFunction(params.callback)){
				params.callback.call(params.scope,record,module.hasOrder);
			}
			search_form.ownerCt.close();
		}
		
		
		function searchInventory(){
			var source_store = source_combo.getStore();
			source_store.load({
				callback:function(){
					var record = params.record;
					search_form.getForm().reset();
					if(!Ext.isEmpty(record)){						
						search_form.getForm().setValues(record);
						var queryParam = encodeFilte({code:record.code,source:record.source,name:record.name,brand:record.brand,carModel:record.carModel});
		    			doRequest(queryParam);
					}
					if(Ext.isEmpty(params.record.code)){
						module.hasOrder = false;
						partner_combox.initializeValue({'name':record.partnerName,'pyCode':record.partnerPyCode,'id':record.partnerIdByBuyer})
					}else{
						module.hasOrder = true;
					}					
				}
			});		
		}
		
		function autoFillDetailForm(){
	    	 var source = this.getValue();
	    	 var code = search_form.getForm().findField("code").getValue();
	    	 if(code){
	    	     var queryParam = {source:source,code:code};
	    	     doRequest(queryParam);
	         }else{
	         	Ext.MessageBox.alert("提示","请填写零件编码");
	       	 }	    		         
	    }
		
	    function doRequest(queryParam){
		    Ext.Ajax.request({
		        url :Uec3.App.Constants.homeUrl+"/part/queryPart!getPartByCodeAndSource.do",
		        params :{queryParam:Ext.encode(queryParam)},
		        method :"post",
		        callback :function(options,success,response){
		             
		        },
		        success:function(response,options){	                	 
				    var resp = Ext.decode(response.responseText);
				    if(resp.success){
			  	 		/*if(resp){
			     			search_form.getForm().setValues(resp);	                	     	          	     	
			  	 		}else{
			     			Ext.MessageBox.alert("提示","此零件编码和来源无相关零件信息,请重试");
			     			search_form.getForm().findField("source").clearValue();
			     			search_form.getForm().findField("name").clearValue();	                	     	
			     		}*/
				    	search_form.getForm().setValues(resp);
				    	if(!resp.hasPro){
				    		Ext.MessageBox.alert("提示",resp.msg,function(){	                	     		
	                	     		delete resp.msg;
	                	     		delete resp.success;
	                	     		delete resp.hasPro;
	                	     		var desktop=Uec3.App.getDesktop();	                	     		
									desktop.launchWindowByParam("partPropertyOperation",{type:'add',data:resp,fn:function(record){
										search_form.getForm().setValues(record);
										search_form.getForm().findField('cost').focus();
									}});
	                	     });
				    	}
				    }else{
				    	Ext.MessageBox.alert("提示",resp.msg);
				  	}        	  				       
		        },
		        failure:function(response,options){
		        	Ext.MessageBox.alert("提示","数据加载异常");
		        }
	    	});	    		    
	    }
	    
	    searchInventory();
	    
		return search_form;
	},
	
	createWindow:function(params){	
		var desktop = this.app.getDesktop();			
		
        var win = desktop.getWindow('pickExpressWindow');
        if (!win) {
			win = desktop.createWindow({
				    id : "pickExpressWindow",
				    title : '零件急件出货',
				    width : 820,
				    height : 380,
					iconCls : 'acc-icon',
				    maximizable : false,
				    resizable:false,
					maximized:false,
					minimizable:false,
					constrainHeader:true,
					modal:true,
					plain:true,
					taskbuttonTooltip : "零件急件出货",
					//layout : "fit",
					items : [this.getComponent(params)]
			});
		}
		win.show();		
	}
});